# app/models.py

from app import db


class Admin(db.Model):
    __tablename__ = 'admininformation'  # 表名
    username = db.Column(db.String(45), primary_key=True)
    password = db.Column(db.String(45), index=True, unique=True)
    def to_dict(self):
        return {
            'username': self.username,  # 将 InstanceState 转换为字典
            'password': self.password,  # 将 InstanceState 转换为字典
        }

class User(db.Model):
    __tablename__ = 'userinfotable'  # 表名
    username = db.Column(db.String(255), primary_key=True)
    password = db.Column(db.String(255), index=True, unique=True)
    name = db.Column(db.String(255), index=True, unique=True)
    gender = db.Column(db.String(45))
    birth = db.Column(db.Date)
    age = db.Column(db.Integer)
    phone = db.Column(db.String(45))
    email = db.Column(db.String(45))
    credit = db.Column(db.String(128))
    friend1 = db.Column(db.String(128))
    friend2 = db.Column(db.String(128))
    friend3 = db.Column(db.String(128))
    def to_dict(self):
        return {
            'username': self.username,  # 将 InstanceState 转换为字典
            'password': self.password,  # 将 InstanceState 转换为字典
            'name': self.name,  # 将 InstanceState 转换为字典
            'gender': self.gender,  # 将 InstanceState 转换为字典
            'birth': self.birth,  # 将 InstanceState 转换为字典
            'age': self.age,  # 将 InstanceState 转换为字典
            'phone': self.phone,  # 将 InstanceState 转换为字典
            'email': self.email,  # 将 InstanceState 转换为字典
            'credit': self.credit,  # 将 InstanceState 转换为字典
            'friend1': self.friend1,  # 将 InstanceState 转换为字典
            'friend2': self.friend2,  # 将 InstanceState 转换为字典
            'friend3': self.friend3,  # 将 InstanceState 转换为字典
        }

class Book(db.Model):
    __tablename__ = 'bookinfotable'  # 表名
    bid = db.Column(db.String(255), primary_key=True)
    bname = db.Column(db.String(255))
    category1 = db.Column(db.String(255))
    category2 = db.Column(db.String(255))
    description = db.Column(db.String)
    isLoaned = db.Column(db.String(255))
    times = db.Column(db.Integer)
    content = db.Column(db.String(255))
    cover = db.Column(db.String)
    price = db.Column(db.DECIMAL(6, 2))
    score = db.Column(db.DECIMAL(3, 1))
    author = db.Column(db.String(255))
    pubUnit = db.Column(db.String(255))

    def to_dict(self):
        return {
            'bid': self.bid,  # 将 InstanceState 转换为字典
            'bname': self.bname,  # 将 InstanceState 转换为字典
            'category1': self.category1,  # 将 InstanceState 转换为字典
            'category2': self.category2,  # 将 InstanceState 转换为字典
            'description': self.description,  # 将 InstanceState 转换为字典
            'isLoaned': self.isLoaned,  # 将 InstanceState 转换为字典
            'times': self.times,  # 将 InstanceState 转换为字典
            'content': self.content,  # 将 InstanceState 转换为字典
            'cover': self.cover,  # 将 InstanceState 转换为字典
            'price': self.price,  # 将 InstanceState 转换为字典
            'score': self.score,  # 将 InstanceState 转换为字典
            'author': self.author,  # 将 InstanceState 转换为字典
            'pubUnit': self.pubUnit  # 将 InstanceState 转换为字典
        }


class Announcement(db.Model):
    __tablename__ = 'announcementtable'  # 表名
    time = db.Column(db.DateTime, primary_key=True)
    title = db.Column(db.String(255))
    content = db.Column(db.String)

    def to_dict(self):
        return {
            'time': str(self.time),
            'title': self.title,
            'content': self.content
        }


class Borrow(db.Model):
    __tablename__ = 'borrowingtable'  # 表名
    bid = db.Column(db.String(255), primary_key=True)
    username = db.Column(db.String(255))
    borrowingTime = db.Column(db.DateTime)
    remainTime = db.Column(db.DateTime)
    score = db.Column(db.DECIMAL(3, 1))

    def to_dict(self):
        return {
            'bid': self.bid,
            'username': self.username,
            'borrowingTime': str(self.borrowingTime),
            'remainTime': str(self.remainTime),
            'score': self.score
        }


class Category(db.Model):
    __tablename__ = 'categorytable'  # 表名
    cid = db.Column(db.String(255), primary_key=True)
    cname = db.Column(db.String(255))
    cover = db.Column(db.String)


class Comment(db.Model):
    __tablename__ = 'commenttable'  # 表名
    bid = db.Column(db.String(255), primary_key=True)
    username = db.Column(db.String(255))
    content = db.Column(db.String)


class IndividualBook:
    __tablename__ = 'individualbooktable'  # 表名
    bid = db.Column(db.String(255), primary_key=True)
    username = db.Column(db.String(255))


class Order:
    __tablename__ = 'individualbooktable'  # 表名
    oid = db.Column(db.String(255), primary_key=True)
    bid = db.Column(db.String(255))
    username = db.Column(db.String(255))
    price = db.Column(db.DECIMAL(6, 2))
    time = db.Column(db.DateTime)
